﻿namespace Agent
{
    using OpenVPN;
    using OpenVPN.States;
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Timers;

    public class VPNConfig : IDisposable
    {
        public EventHandler<LogEventArgs> a;
        public EventHandler<TmpFileLogEventArgs> b;
        public EventHandler<NeedPasswordEventArgs> c;
        public EventHandler<NeedLoginAndPasswordEventArgs> d;
        private Connection e;
        private string f;
        private string g;
        private string h;
        private int i;
        private Timer j = new Timer(100.0);
        private Timer k;
        private string l = Path.GetTempFileName();
        private bool m;
        private ConnectionButton n;
        private FileSystemWatcher o = new FileSystemWatcher();
        private Guid p;
        private long q;
        private bool r;
        [CompilerGenerated]
        private string s;

        private VPNConfig()
        {
            this.j.Elapsed += new ElapsedEventHandler(this.f);
        }

        public void Dispose()
        {
            this.e(true);
            GC.SuppressFinalize(this);
        }

        private void e()
        {
            string str = "";
            FileStream stream = File.Open(this.s(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            try
            {
                this.q = stream.Seek(this.q, SeekOrigin.Begin);
                if (stream.Length < this.q)
                {
                    this.q = 0L;
                }
                byte[] buffer = new byte[stream.Length - this.q];
                this.q += stream.Read(buffer, 0, (int) (stream.Length - this.q));
                str = Encoding.Default.GetString(buffer);
                if (this.b != null)
                {
                    TmpFileLogEventArgs e = new TmpFileLogEventArgs();
                    e.a(str);
                    this.b(this, e);
                }
            }
            catch (Exception exception)
            {
                string message = exception.Message;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
        }

        private void e(bool A_0)
        {
            if (!this.r)
            {
                ConfigManager.e(this.p.ToString());
                File.Delete(this.l);
                if (A_0)
                {
                    this.j.Dispose();
                    this.e.Dispose();
                }
                this.e = null;
                this.j = null;
                this.r = true;
            }
        }

        private void e(object A_0, LogEventArgs A_1)
        {
            if (this.a != null)
            {
                this.a(A_0, A_1);
            }
        }

        private void e(object A_0, NeedCardIdEventArgs A_1)
        {
        }

        private void e(object A_0, NeedLoginAndPasswordEventArgs A_1)
        {
            if (this.d != null)
            {
                this.d(A_0, A_1);
                if ((string.IsNullOrWhiteSpace(A_1.Password) || string.IsNullOrWhiteSpace(A_1.UserName)) && (this.l().State.CreateSnapshot().ConnectionState == VPNConnectionState.Initializing))
                {
                    this.j.Start();
                }
            }
        }

        private void e(object A_0, NeedPasswordEventArgs A_1)
        {
        }

        private void e(object A_0, EventArgs A_1)
        {
            this.o();
        }

        private void e(object A_0, ElapsedEventArgs A_1)
        {
            if (this.e.State.CreateSnapshot().ConnectionState == VPNConnectionState.Stopped)
            {
                ((Timer) A_0).Stop();
                this.i();
            }
        }

        public static VPNConfig e(string A_0, int A_1)
        {
            VPNConfig config = new VPNConfig {
                g = A_0,
                i = A_1,
                m = true
            };
            config.f();
            return config;
        }

        public static VPNConfig e(string A_0, string A_1, int A_2)
        {
            VPNConfig config = new VPNConfig {
                g = A_1,
                h = A_0,
                i = A_2,
                m = false
            };
            config.f();
            return config;
        }

        private void f()
        {
            this.e = null;
            try
            {
                if (!this.m)
                {
                    this.e = new UserSpaceConnection(this.h, this.g, this.l, new EventHandler<LogEventArgs>(this.e), this.i);
                    this.e.NeedLoginAndPassword += new EventHandler<NeedLoginAndPasswordEventArgs>(this.e);
                }
                else
                {
                    this.e = new ServiceConnection(this.g, new EventHandler<LogEventArgs>(this.e), this.i);
                    this.e.NeedLoginAndPassword += new EventHandler<NeedLoginAndPasswordEventArgs>(this.e);
                }
            }
            catch (ApplicationException exception)
            {
                this.f = exception.Message;
            }
            this.e.Logs.DebugLevel = this.i;
        }

        private void f(object A_0, EventArgs A_1)
        {
            if (this.e.State.CreateSnapshot().ConnectionState == VPNConnectionState.Stopped)
            {
                this.i();
            }
        }

        private void f(object A_0, ElapsedEventArgs A_1)
        {
            ((Timer) A_0).Stop();
            this.k();
        }

        ~VPNConfig()
        {
            this.e(false);
        }

        private void g(object A_0, EventArgs A_1)
        {
            this.j();
        }

        private void h(object A_0, EventArgs A_1)
        {
            this.r();
        }

        public void i()
        {
            try
            {
                this.e.Connect();
                this.n();
            }
            catch (InvalidOperationException)
            {
            }
        }

        private void i(object A_0, EventArgs A_1)
        {
            VPNConnectionState connectionState = this.e.State.CreateSnapshot().ConnectionState;
            Connection e = this.e;
            this.k();
            this.f();
        }

        public void j()
        {
            ProcessStartInfo info = new ProcessStartInfo {
                Arguments = "\"" + this.g + "\"",
                ErrorDialog = true,
                FileName = "notepad.exe",
                UseShellExecute = true
            };
            Process process = new Process {
                StartInfo = info,
                EnableRaisingEvents = true
            };
            process.Exited += new EventHandler(this.i);
            process.Start();
        }

        public void j(object A_0, EventArgs A_1)
        {
            switch (this.e.State.CreateSnapshot().ConnectionState)
            {
                case VPNConnectionState.Initializing:
                case VPNConnectionState.Running:
                    this.k();
                    break;
            }
        }

        public void k()
        {
            if (this.e != null)
            {
                try
                {
                    this.e.Disconnect();
                }
                catch (InvalidOperationException)
                {
                }
            }
        }

        public void m()
        {
            VPNConnectionState connectionState = this.e.State.CreateSnapshot().ConnectionState;
            Connection e = this.e;
            this.k();
            this.f();
        }

        protected void n()
        {
            this.p = Guid.NewGuid();
            ConfigManager.a(this.p.ToString(), 100.0, new System.Action(this.e));
        }

        public void o()
        {
        }

        public void r()
        {
        }

        public void t()
        {
            this.k();
            this.k = new Timer(100.0);
            this.k.Start();
            this.k.Elapsed += new ElapsedEventHandler(this.e);
        }

        public string ConfigFile
        {
            get
            {
                return this.g;
            }
        }

        public bool IsService
        {
            get
            {
                return this.m;
            }
        }

        public string Name
        {
            [CompilerGenerated]
            get
            {
                return this.s;
            }
            [CompilerGenerated]
            private set
            {
                this.s = value;
            }
        }

        public bool Running
        {
            get
            {
                return ((this.e != null) && (this.e.State.CreateSnapshot().ConnectionState != VPNConnectionState.Stopped));
            }
        }

        public string TmpFileName
        {
            get
            {
                return this.l;
            }
        }

        public Connection VPNConnection
        {
            get
            {
                return this.e;
            }
        }
    }
}

